package top.wardyan.wdev.dbdevassist.action.base;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;

import top.wardyan.base.util.DateUtil;
import top.wardyan.conf.Const;
import top.wardyan.control.WButton;
import top.wardyan.control.WComposite;
import top.wardyan.control.WLabel;
import top.wardyan.control.WText;
import top.wardyan.listener.WNumberOnlyKeyAdapter;
import top.wardyan.util.Msg;
import top.wardyan.util.ShellUtil;
import top.wardyan.wdev.dbdevassist.config.DDA;
import top.wardyan.wdev.dbdevassist.model.DetailModel;
import top.wardyan.wdev.dbdevassist.model.YTDic;
import top.wardyan.wdev.dbdevassist.model.YTObjectPara;
import top.wardyan.wdev.dbdevassist.service.IDetailService;

public class ParaEditShell<T extends ParaEditShell<T, E>, E extends DetailModel<E>> extends BaseDetailEditShell<T, E>{
	
	WComposite comMain;
	WText txtSIndex;
	WText txtXDicName;
	WButton btnDicSelect;
	WText txtXField;
	WText txtXName;
	WText txtXDesc;
	WText txtXHint; 
	WText txtXNote;
	
	YTDic dic = null;
	

	public ParaEditShell(int mID, int dID, IDetailService<E> detailService) {
		super(mID, dID, detailService);
		this.createContent();
	}
	
	private void createContent() {
		// 序号 字典 参数 名称 描述 提示 备注
		//this.setSize(400, this.getSize().y);
		comMain = new WComposite(this, SWT.BORDER);
		comMain.setLayout(new FormLayout());
		FormData fd_comMain = new FormData();
		fd_comMain.left = new FormAttachment(0);
		fd_comMain.top = new FormAttachment(self.topCom, 5);
		fd_comMain.right = new FormAttachment(100, -1);
		fd_comMain.bottom = new FormAttachment(100, -1);
		comMain.setLayoutData(fd_comMain);
		
		WComposite comSIndex = new WComposite(comMain, SWT.NONE);
		comSIndex.setLayout(new FormLayout());
		FormData fd_comSIndex = new FormData();
		fd_comSIndex.left = new FormAttachment(comMain, 0);
		fd_comSIndex.top = new FormAttachment(comMain, 2);
		fd_comSIndex.right = new FormAttachment(50, -1);
		fd_comSIndex.bottom = new FormAttachment(comSIndex, 35);
		comSIndex.setLayoutData(fd_comSIndex);
		WLabel lblSIndex = new WLabel(comSIndex, SWT.BORDER, Const.FONT16, "序号", SWT.RIGHT);
		FormData fd_lblSIndex = new FormData();
		fd_lblSIndex.left = new FormAttachment(comSIndex, 0);
		fd_lblSIndex.top = new FormAttachment(comSIndex, 2);
		fd_lblSIndex.right = new FormAttachment(30, -1);
		fd_lblSIndex.bottom = new FormAttachment(lblSIndex, 30);
		lblSIndex.setLayoutData(fd_lblSIndex);
		txtSIndex = new WText(comSIndex, SWT.BORDER, "", "序号");
		txtSIndex.setTextLimit(9);
		txtSIndex.addKeyListener(new WNumberOnlyKeyAdapter());
		FormData fd_txtSIndex = new FormData();
		fd_txtSIndex.left = new FormAttachment(lblSIndex, 4);
		fd_txtSIndex.top = new FormAttachment(comSIndex, 2);
		fd_txtSIndex.right = new FormAttachment(100, -8);
		fd_txtSIndex.bottom = new FormAttachment(txtSIndex, 30);
		txtSIndex.setLayoutData(fd_txtSIndex);
		
		WComposite comXDicName = new WComposite(comMain, SWT.NONE);
		comXDicName.setLayout(new FormLayout());
		FormData fd_comXDicName = new FormData();
		fd_comXDicName.left = new FormAttachment(comSIndex, 4);
		fd_comXDicName.top = new FormAttachment(comMain, 0);
		fd_comXDicName.right = new FormAttachment(100, -4);
		fd_comXDicName.bottom = new FormAttachment(comXDicName, 35);
		comXDicName.setLayoutData(fd_comXDicName);
		WLabel lblXDicName = new WLabel(comXDicName, SWT.BORDER, Const.FONT16, "字典", SWT.RIGHT);
		FormData fd_lblXDicName = new FormData();
		fd_lblXDicName.left = new FormAttachment(comXDicName, 0);
		fd_lblXDicName.top = new FormAttachment(comXDicName, 2);
		fd_lblXDicName.right = new FormAttachment(30, 0);
		fd_lblXDicName.bottom = new FormAttachment(lblXDicName, 30);
		lblXDicName.setLayoutData(fd_lblXDicName);
		txtXDicName = new WText(comXDicName, SWT.BORDER, "", "字典");
		txtXDicName.setTextLimit(128);
		FormData fd_txtXDicName = new FormData();
		fd_txtXDicName.left = new FormAttachment(lblXDicName, 4);
		fd_txtXDicName.top = new FormAttachment(comXDicName, 2);
		fd_txtXDicName.right = new FormAttachment(90, -8);
		fd_txtXDicName.bottom = new FormAttachment(txtXDicName, 30);
		txtXDicName.setLayoutData(fd_txtXDicName);
		
		btnDicSelect = new WButton(comXDicName, SWT.PUSH);
		btnDicSelect.setText("*");
		btnDicSelect.setAlignment(SWT.CENTER);
		FormData fd_btnDicSelect = new FormData();
		fd_btnDicSelect.left = new FormAttachment(txtXDicName, 2);
		fd_btnDicSelect.top = new FormAttachment(comXDicName, 2);
		fd_btnDicSelect.right = new FormAttachment(100, -8);
		fd_btnDicSelect.bottom = new FormAttachment(txtXDicName, 30);
		btnDicSelect.setLayoutData(fd_btnDicSelect);
		btnDicSelect.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				int rDic_ID = new DicSelectDialog(self, SWT.NONE).open();
				if(rDic_ID > 0) {
					self.dic = dda.dicService.findByID(rDic_ID);
					if(self.dic != null && self.dic.getXName() != null) {
						txtXDicName.setText(self.dic.getXName());
					}else {
						txtXDicName.setText("");
					}
				}
			}
		});
		
		WComposite comXField = new WComposite(comMain, SWT.NONE);
		comXField.setLayout(new FormLayout());
		FormData fd_comXField = new FormData();
		fd_comXField.left = new FormAttachment(comMain, 0);
		fd_comXField.top = new FormAttachment(comSIndex, 2);
		fd_comXField.right = new FormAttachment(50, 0);
		fd_comXField.bottom = new FormAttachment(comXField, 35);
		comXField.setLayoutData(fd_comXField);
		
		WLabel lblXField = new WLabel(comXField, SWT.BORDER, Const.FONT16, "名称", SWT.RIGHT);
		FormData fd_lblXField = new FormData();
		fd_lblXField.left = new FormAttachment(comXField, 0);
		fd_lblXField.top = new FormAttachment(comXField, 2);
		fd_lblXField.right = new FormAttachment(30, 0);
		fd_lblXField.bottom = new FormAttachment(lblXField, 30);
		lblXField.setLayoutData(fd_lblXField);
		txtXField = new WText(comXField, SWT.BORDER, "", "名称");
		txtXField.setTextLimit(128);
		FormData fd_txtXField = new FormData();
		fd_txtXField.left = new FormAttachment(lblXField, 4);
		fd_txtXField.top = new FormAttachment(comXField, 2);
		fd_txtXField.right = new FormAttachment(100, -8);
		fd_txtXField.bottom = new FormAttachment(txtXField, 30);
		txtXField.setLayoutData(fd_txtXField);
		
		
		WComposite comXName = new WComposite(comMain, SWT.NONE);
		comXName.setLayout(new FormLayout());
		FormData fd_comXName = new FormData();
		fd_comXName.left = new FormAttachment(comXField, 4);
		fd_comXName.top = new FormAttachment(comXDicName, 0);
		fd_comXName.right = new FormAttachment(100, 0);
		fd_comXName.bottom = new FormAttachment(comXName, 35);
		comXName.setLayoutData(fd_comXName);
		
		WLabel lblXName = new WLabel(comXName, SWT.BORDER, Const.FONT16, "名称", SWT.RIGHT);
		FormData fd_lblXName = new FormData();
		fd_lblXName.left = new FormAttachment(comXName, 0);
		fd_lblXName.top = new FormAttachment(comXName, 2);
		fd_lblXName.right = new FormAttachment(30, 0);
		fd_lblXName.bottom = new FormAttachment(lblXName, 30);
		lblXName.setLayoutData(fd_lblXName);
		txtXName = new WText(comXName, SWT.BORDER, "", "名称");
		txtXName.setTextLimit(128);
		FormData fd_txtXName = new FormData();
		fd_txtXName.left = new FormAttachment(lblXName, 4);
		fd_txtXName.top = new FormAttachment(comXName, 2);
		fd_txtXName.right = new FormAttachment(100, -8);
		fd_txtXName.bottom = new FormAttachment(txtXName, 30);
		txtXName.setLayoutData(fd_txtXName);
		
		
		//////////////////////
		WComposite comXDesc = new WComposite(comMain, SWT.NONE);
		comXDesc.setLayout(new FormLayout());
		FormData fd_comXDesc = new FormData();
		fd_comXDesc.left = new FormAttachment(comMain, 0);
		fd_comXDesc.top = new FormAttachment(comXField, 2);
		fd_comXDesc.right = new FormAttachment(50, 0);
		fd_comXDesc.bottom = new FormAttachment(comXDesc, 35);
		comXDesc.setLayoutData(fd_comXDesc);
		
		WLabel lblXDesc = new WLabel(comXDesc, SWT.BORDER, Const.FONT16, "描述", SWT.RIGHT);
		FormData fd_lblXDesc = new FormData();
		fd_lblXDesc.left = new FormAttachment(comXDesc, 0);
		fd_lblXDesc.top = new FormAttachment(comXDesc, 2);
		fd_lblXDesc.right = new FormAttachment(30, 0);
		fd_lblXDesc.bottom = new FormAttachment(lblXDesc, 30);
		lblXDesc.setLayoutData(fd_lblXDesc);
		txtXDesc = new WText(comXDesc, SWT.BORDER, "", "描述");
		txtXDesc.setTextLimit(128);
		FormData fd_txtXDesc = new FormData();
		fd_txtXDesc.left = new FormAttachment(lblXDesc, 4);
		fd_txtXDesc.top = new FormAttachment(comXDesc, 2);
		fd_txtXDesc.right = new FormAttachment(100, -8);
		fd_txtXDesc.bottom = new FormAttachment(txtXDesc, 30);
		txtXDesc.setLayoutData(fd_txtXDesc);
		
		
		WComposite comXHint = new WComposite(comMain, SWT.NONE);
		comXHint.setLayout(new FormLayout());
		FormData fd_comXHint = new FormData();
		fd_comXHint.left = new FormAttachment(comXDesc, 4);
		fd_comXHint.top = new FormAttachment(comXDicName, 0);
		fd_comXHint.right = new FormAttachment(100, 0);
		fd_comXHint.bottom = new FormAttachment(comXHint, 35);
		comXHint.setLayoutData(fd_comXHint);
		
		WLabel lblXHint = new WLabel(comXHint, SWT.BORDER, Const.FONT16, "提示", SWT.RIGHT);
		FormData fd_lblXHint = new FormData();
		fd_lblXHint.left = new FormAttachment(comXHint, 0);
		fd_lblXHint.top = new FormAttachment(comXHint, 2);
		fd_lblXHint.right = new FormAttachment(30, 0);
		fd_lblXHint.bottom = new FormAttachment(lblXHint, 30);
		lblXHint.setLayoutData(fd_lblXHint);
		txtXHint = new WText(comXHint, SWT.BORDER, "", "提示");
		txtXHint.setTextLimit(128);
		FormData fd_txtXHint = new FormData();
		fd_txtXHint.left = new FormAttachment(lblXHint, 4);
		fd_txtXHint.top = new FormAttachment(comXHint, 2);
		fd_txtXHint.right = new FormAttachment(100, -8);
		fd_txtXHint.bottom = new FormAttachment(txtXHint, 30);
		txtXHint.setLayoutData(fd_txtXHint);
		
		//
		WComposite comXNote = new WComposite(comMain, SWT.NONE);
		comXNote.setLayout(new FormLayout());
		FormData fd_comXNote = new FormData();
		fd_comXNote.left = new FormAttachment(comMain, 0);
		fd_comXNote.top = new FormAttachment(comXHint, 0);
		fd_comXNote.right = new FormAttachment(50, 0);
		fd_comXNote.bottom = new FormAttachment(comXNote, 35);
		comXNote.setLayoutData(fd_comXNote);
		
		WLabel lblXNote = new WLabel(comXNote, SWT.BORDER, Const.FONT16, "备注", SWT.RIGHT);
		FormData fd_lblXNote = new FormData();
		fd_lblXNote.left = new FormAttachment(comXNote, 0);
		fd_lblXNote.top = new FormAttachment(comXNote, 2);
		fd_lblXNote.right = new FormAttachment(30, 0);
		fd_lblXNote.bottom = new FormAttachment(lblXNote, 30);
		lblXNote.setLayoutData(fd_lblXNote);
		txtXNote = new WText(comXNote, SWT.BORDER, "", "备注");
		txtXNote.setTextLimit(128);
		FormData fd_txtXNote = new FormData();
		fd_txtXNote.left = new FormAttachment(lblXNote, 4);
		fd_txtXNote.top = new FormAttachment(comXNote, 2);
		fd_txtXNote.right = new FormAttachment(100, -8);
		fd_txtXNote.bottom = new FormAttachment(txtXNote, 30);
		txtXNote.setLayoutData(fd_txtXNote);
		
		
		//self.layout();
		this.show();
		
		self.pack();
		ShellUtil.center(self);
	}
	

	@Override
	protected void initBtnStatus(boolean isInit) {
		super.initBtnStatus(isInit);
		self.btnAdd.setEnabled(false);
	}

	@Override
	protected boolean checkModelChange() {
		boolean rs = false;
		if(txtXHint == null)
			return rs;
		if(model != null) {
			rs = !model.getStr("xHint").equals(txtXHint.getText())
					|| !model.getStr("xNote").equals(txtXNote.getText())
					|| !model.getStr("xDesc").equals(txtXDesc.getText())
					|| !model.getStr("sIndex").equals(txtSIndex.getText())
					|| !model.getStr("xName").equals(txtXName.getText())
					|| !model.getStr("xField").equals(txtXField.getText())
					|| (self.dic != null && !model.getInt("rDic_ID").equals(self.dic.getSID()));
		}else {
			rs = !txtXHint.getText().equals("") && !txtXDesc.getText().equals("") && !txtXNote.getText().equals("")
					&& !txtSIndex.getText().equals("")&& !txtXName.getText().equals("")
					&& !txtXField.getText().equals("");
		}
		return rs;
	}
	
	private boolean checkinput() {
		int index = 0;
		boolean rs = true;
		try {
			index = Integer.valueOf(txtSIndex.getText());
			if(!(index > 0)) {
				rs = false;
				Msg.show("错误", "序号错误");
			}
		}catch(Exception e) {
			rs = false;
			Msg.show("错误", "序号错误");
		}
		if(rs && txtXField.getText().length() == 0) {
			rs = false;
			Msg.show("错误", "名称错误");
		}
//		if(rs && txtXName.getText().length() == 0) {
//			rs = false;
//			Msg.show("错误", "名称错误");
//		}
		return rs;
	}

	@SuppressWarnings("unchecked")
	@Override
	protected boolean save() {
		if(this.checkinput()) {
			if(this.model == null || this.dID <= 0) {
				this.model = (E) new YTObjectPara().setMID(this.mID)
						.setDID(detailService.genDID(this.mID))
						.setRAuthorID(dda.user.getSID())
						;
			}
			this.model.setSIndex(Integer.valueOf(this.txtSIndex.getText()))
			.setRDicID(dic != null ? dic.getSID() : 0)
			.setSCreateTime(DateUtil.getCurrentTimestamp())
			.setSLastUpdate(DateUtil.getCurrentTimestamp())
			.setSStatus(2)
			.setSVersion(1)
			.setXDesc(this.txtXDesc.getText())
			.setXField(this.txtXField.getText())
			.setXName(this.txtXName.getText())
			.setXNote(this.txtXNote.getText())
			//.setXTableName("")
			;
			if(this.dID <= 0)
				return detailService.save(model);
			else
				return detailService.update(model);
		}
		return false;
	}

	@Override
	protected void show() {
		this.txtSIndex.removeModifyListener(this.modifyListener);
		this.txtXField.removeModifyListener(this.modifyListener);
		this.txtXName.removeModifyListener(this.modifyListener);
		this.txtXNote.removeModifyListener(this.modifyListener);
		this.txtXDesc.removeModifyListener(this.modifyListener);
		this.txtXHint.removeModifyListener(this.modifyListener);
		this.txtXDicName.removeModifyListener(this.modifyListener);
		
		if(this.model != null && dID > 0) {
			txtSIndex.setText(model.getSIndex() > 0 ? String.valueOf(model.getSIndex()) : String.valueOf(this.detailService.genSIndex(self.mID)));
			txtXField.setText(model.getXField() != null ? model.getXField() : "");
			txtXName.setText(model.getXName() != null ? model.getXName() : "");
			txtXNote.setText(model.getXNote() != null ? model.getXNote() : "");
			txtXDesc.setText(model.getXDesc() != null ? model.getXDesc() : "");
			txtXHint.setText(model.getXHint() != null ? model.getXHint() : "");
			txtXDicName.setText(model.getRDicID() != null && model.getRDicID().getXName() != null ? model.getRDicID().getXName() : "");
			//this.txtXDicName.setEnabled(false);
			this.txtXDicName.setEditable(false);
		}else {
			txtSIndex.setText(String.valueOf(this.detailService.genSIndex(self.mID)));
			txtXField.setText("");
			txtXName.setText("");
			txtXNote.setText("");
			txtXDesc.setText("");
			txtXHint.setText("");
			txtXDicName.setText("");
		}
		
		this.txtSIndex.addModifyListener(this.modifyListener);
		this.txtXField.addModifyListener(this.modifyListener);
		this.txtXName.addModifyListener(this.modifyListener);
		this.txtXNote.addModifyListener(this.modifyListener);
		this.txtXDesc.addModifyListener(this.modifyListener);
		this.txtXHint.addModifyListener(this.modifyListener);
		this.txtXDicName.addModifyListener(this.modifyListener);
	}

//	public ParaEditShell() {
//		this(1, 3, DDA.getInstance().objectParaService);
//	}
	
	public static void main(String[] args) {
		new ParaEditShell(1, 3, DDA.getInstance().objectParaService).go();
	}
}
